package cn.mado.org.controller;

import cn.mado.basic.note.SysLog;
import cn.mado.basic.util.AjaxResult;
import cn.mado.basic.util.PageList;
import cn.mado.org.domain.Dept;
import cn.mado.org.domain.Novel;
import cn.mado.org.query.DeptQuery;
import cn.mado.org.query.NovelQuery;
import cn.mado.org.service.DeptService;
import cn.mado.org.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/novel")
public class NovelController {

    @Autowired
    private NovelService novelService;

    /**
     * 查询部门全部
     */
    @GetMapping
    public List<Novel> findAll() {
        return novelService.findNoveAll();
    }

    /**
     * 添加、修改部门
     */
    @SysLog("新增修改了一本小说")
    @PostMapping("save")
    public AjaxResult addOrUpdate(@RequestBody Novel novel){
        System.out.println("我来："+novel);
        if (null == novel.getNovelId()){
            // 添加
            novelService.addnovel(novel);
        }else {
            // 修改
            novelService.updatanovel(novel);
        }
        return AjaxResult.me().success();
    }
    /**
     * 删除 delete localhost:8080/novel/1
     */
    @SysLog("删除了一本小说")
    @DeleteMapping("/{novelId}")
    public AjaxResult delete(@PathVariable("novelId") Long novelId){
        System.out.println(novelId);
        novelService.delnovelByid(novelId);
        return AjaxResult.me().success();
    }


    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<Novel> queryByPage(@RequestBody NovelQuery query){
        List<Novel> nove = novelService.findNove(query);
        // System.out.println(nove);
        Integer integer = novelService.findnovelTotals(query);
        // System.out.println("条数："+integer);
        return new PageList<>(integer,nove);
    }
}
